<?php
	class ResultatManager{
		private $_db;
		
		public function __construct($db){
				$this->setDb($db);	
		}
		
		public function setDb(PDO $db){
				$this->_db = $db;
		}		
		
		public function add(Resultat $resultat){// permet d'ajouter un réslutat
				$q = $this->_db->prepare('INSERT INTO resultat SET idRefEleve = :idRefEleve, idRefTest = :idRefTest, idRefReponse = :idRefReponse, temps = :temps');
				$q->bindValue(':idRefEleve', $resultat->idRefEleve());
				$q->bindValue(':idRefTest', $resultat->idRefTest());
				$q->bindValue(':idRefReponse', $resultat->idRefReponse());
				$q->bindValue(':temps', $resultat->temps());
				$q->execute();			
		}
		
		public function delete(Resultat $resultat){// permet de supprimer un résultat
				$this->_db->exec('DELETE FROM resultat WHERE id = '.$resultat->id());
		}
		
		public function update(Resultat $resultat){// permet de modifier un résultat
				$q = $this->_db->prepare('UPDATE resultat SET idRefEleve = :idRefEleve, idRefTest = :idRefTest, idRefReponse = :idRefReponse');
				$q->bindValue(':idRefEleve', $resultat->idRefEleve());
				$q->bindValue(':idRefTest', $resultat->idRefTest());
				$q->bindValue(':idRefReponse', $resultat->idRefReponse());
				$q->execute();			
		}

		public function get($id){// retourne l'objet résultat d'id correspondant
				$id = (int) $id;
				$q = $this->_db->query('SELECT * FROM resultat WHERE id = '.$id);
				$donnees = $q->fetch();
				return new Resultat($donnees);			
		}

		public function getByEleve($idEleve){// retourne un tableau des résultats correspondant à un élève.
				$resultat = array();
				$q = $this->_db->query('SELECT * FROM resultat WHERE idRefEleve = '.$idEleve);
				while ($donnees = $q->fetch()){
					$resultat[] = new Resultat($donnees);
				}
				return $resultat;				
		}
		
		public function getByClasse($idClasse){// retourne un tableau des résultats correspondant à une classe.
				$resultat = array();
				$q = $this->_db->query('SELECT resultat.* FROM resultat, eleve WHERE resultat.idRefEleve = eleve.id AND eleve.idRefClasse = '.$idClasse);
				while ($donnees = $q->fetch()){
					$resultat[] = new Resultat($donnees);
				}
				return $resultat;				
		}

		public function getListEleve() { //retourne un tableau contenant les élèves ayant des résultats
				$resultat = array();
				$q = $this->_db->query('SELECT * FROM resultat GROUP BY idRefEleve, idRefTest');
				while ($donnees = $q->fetch()){
					$resultat[] = new Resultat($donnees);
				}
				return $resultat;
		}
		
		public function getListEleveByClasse($idClasse) { //retourne un tableau contenant les élèves ayant des résultats en fonction de la classe
				$resultat = array();
				$q = $this->_db->query('SELECT resultat.* FROM resultat, eleve WHERE resultat.idRefEleve = eleve.id AND eleve.idRefClasse = '.$idClasse.' GROUP BY idRefEleve');
				while ($donnees = $q->fetch()){
					$resultat[] = new Resultat($donnees);
				}
				return $resultat;
		}

		public function getExercice($id) { //Retourne l'exercice correspondant à un résultat
				$q = $this->_db->query('SELECT exercice.* FROM  exercice, question, reponse, resultat WHERE resultat.id = '.$id.' AND resultat.idRefReponse = reponse.id AND reponse.idRefQuestion = question.id AND question.idRefExercice = exercice.id');
				$donnees = $q->fetch();
				return new Exercice($donnees);				
		}

		public function ListExercice($idEleve, $idTest) { //Retourne la liste des exercices déjà effectués pour un test
				$arrEx = array();
				$q = $this->_db->query('SELECT * FROM resultat WHERE idRefEleve = '.$idEleve.' AND idRefTest = '.$idTest);
				while($donnees = $q->fetch()){
					$arrEx[] = $this->getExercice($donnees['id'])->id();
				}
				return array_unique($arrEx);
		}
		
		public function ListExerciceClasse($idClasse) { //Retourne la liste des exercices déjà effectués pour une classe
				$arrEx = array();
				$q = $this->_db->query('SELECT resultat.* FROM resultat, eleve WHERE resultat.idRefEleve = eleve.id AND eleve.idRefClasse = '.$idClasse);
				while($donnees = $q->fetch()){
					$arrEx[] = $this->getExercice($donnees['id'])->id();
				}
				return array_unique($arrEx);
		}
		
		public function getByExerciceEleveTest($idExercice, $idEleve, $idTest) { //Retourne les résultats en fonction d'un exercice et d'un élève
				$resultat = array();
				$q = $this->_db->query('SELECT resultat.* FROM resultat, exercice, reponse, question WHERE resultat.idRefReponse = reponse.id AND reponse.idRefQuestion = question.id AND question.idRefExercice = exercice.id AND exercice.id = '.$idExercice.' AND resultat.idRefEleve = '.$idEleve.' AND resultat.idRefTest = '.$idTest);
				while ($donnees = $q->fetch()){
					$resultat[] = new Resultat($donnees);
				}
				return $resultat;	
		}
		
		public function getByExercice($idExercice) { //Retourne les résultats en fonction d'un exercice et d'un élève
				$resultat = array();
				$q = $this->_db->query('SELECT resultat.* FROM resultat, exercice, reponse, question WHERE resultat.idRefReponse = reponse.id AND reponse.idRefQuestion = question.id AND question.idRefExercice = exercice.id AND exercice.id = '.$idExercice);
				while ($donnees = $q->fetch()){
					$resultat[] = new Resultat($donnees);
				}
				return $resultat;	
		}
		
		public function getByQuestion($idQuestion, $idEleve) { //Retourne les résultats en fonction d'une question et d'un élève
				$resultat = array();
				$q = $this->_db->query('SELECT resultat.* FROM resultat, reponse, question WHERE resultat.idRefReponse = reponse.id AND reponse.idRefQuestion = question.id AND question.id = '.$idQuestion.' AND resultat.idRefEleve = '.$idEleve);
				while ($donnees = $q->fetch()){
					$resultat[] = new Resultat($donnees);
				}
				return $resultat;	
		}
		
		public function getByQuestionByClasse($idQuestion, $idClasse) { //Retourne les résultats en fonction d'une question et d'une classe
				$resultat = array();
				$q = $this->_db->query('SELECT resultat.* FROM resultat, reponse, question, eleve WHERE resultat.idRefReponse = reponse.id AND reponse.idRefQuestion = question.id AND question.id = '.$idQuestion.' AND resultat.idRefEleve = eleve.id AND eleve.idRefClasse = '.$idClasse);
				while ($donnees = $q->fetch()){
					$resultat[] = new Resultat($donnees);
				}
				return $resultat;	
		}
		
		public function getQuestion($idExercice, $idEleve)
		{
			$question = array();
			$q = $this->_db->query('SELECT question.* FROM resultat, reponse, question, exercice WHERE resultat.idRefReponse = reponse.id AND reponse.idRefQuestion = question.id AND resultat.idRefEleve = '.$idEleve.' AND question.idRefExercice = '.$idExercice.' GROUP BY question.id');
			while ($donnees = $q->fetch()){
				$question[] = new Question($donnees);
			}
			return $question;			
		}
		
		public function getQuestionByClasse($idExercice, $idClasse)
		{
			$question = array();
			$q = $this->_db->query('SELECT question.* FROM resultat, reponse, question, exercice, eleve WHERE resultat.idRefReponse = reponse.id AND reponse.idRefQuestion = question.id AND resultat.idRefEleve = eleve.id AND eleve.idRefClasse = '.$idClasse.' AND question.idRefExercice = '.$idExercice.' GROUP BY question.id');
			while ($donnees = $q->fetch()){
				$question[] = new Question($donnees);
			}
			return $question;			
		}
		
		public function reinitializeExercice($idEleve, $idExercice, $idTest) //Réinitialise un exercice pour un élève (point de reprise)
		{
			$arrRes = $this->getByExerciceEleveTest($idExercice, $idEleve, $idTest);
			foreach($arrRes as $res)
			{
				$this->delete($res);
			}
		}
		
		
		
		//////////////////////////
		// Calcul des résultats //
		//////////////////////////
		
		public function TxReponseCorrecte($idExercice) { //Retourne le taux de réponse correcte pour un exercice
			$exercicemanager = new ExerciceManager($this->_db);
			$p = $this->_db->query('SELECT COUNT(*) FROM resultat, exercice, reponse, question WHERE resultat.idRefReponse = reponse.id AND reponse.idRefQuestion = question.id AND question.idRefExercice = exercice.id AND exercice.id = '.$idExercice.' AND reponse.correcte = 1');

			$repJuste = $p->fetch();
			
			$resultat = array();
			$q = $this->_db->query('SELECT resultat.* FROM resultat, reponse, question WHERE resultat.idRefReponse = reponse.id AND reponse.idRefQuestion = question.id AND question.idRefExercice GROUP BY question.idRefExercice');
			while ($donnees = $q->fetch()){
				$resultat[] = new Resultat($donnees);
			}
			
			$rep = $exercicemanager->getNbReponseJuste($idExercice)*sizeof($resultat);
			return ($repJuste[0]/$rep)*100;
		}
		
		public function TxReponse($idExercice) { //Retourne le taux de réponse correcte pour un exercice
			$exercicemanager = new ExerciceManager($this->_db);
			$p = $this->_db->query('SELECT COUNT(*) FROM resultat, exercice, reponse, question WHERE resultat.idRefReponse = reponse.id AND reponse.idRefQuestion = question.id AND question.idRefExercice = exercice.id AND exercice.id = '.$idExercice);

			$rep = $p->fetch();
			$reptot = $exercicemanager->getNbReponse($idExercice)*sizeof($this->getListEleve());
			return ($rep[0]/$reptot)*100;
		}

	}
?>